﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Admin.Comment
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<Models.Discussion> data;
            long groupID = Request.QueryString["DiscussionGroupID"].ToInt64TryParse(0);
            data = DiscussionBLL.GetByCondition(groupId: ((groupID == 0) ? -1 : groupID)).ToList();
            //data = DiscussionBLL.GetAll().ToList() ;
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<Models.Discussion>(data, fc, e);
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                long ID = Convert.ToInt64(sm.SelectedRows[0].RecordID);
                //DiscussionAddEdit1.BindData(ID);
            }

        }
        protected void btnUpdateEnabled_Click(object sender, DirectEventArgs e)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                long ID = Convert.ToInt64(row.RecordID);
                BitPortal.Models.Discussion Discussion = bitEnt.Discussions.SingleOrDefault(ent => ent.DiscussionID == ID);
                if (Discussion != null)
                    Discussion.IsEnabled = true;
            }
            int record = bitEnt.SaveChanges();
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Cập nhật " + record + " bài viết thành công!"
            });
            LoadGridData();
        }

        protected void btnUpdateNotEnabled_Click(object sender, DirectEventArgs e)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                long ID = Convert.ToInt64(row.RecordID);
                BitPortal.Models.Discussion Discussion = bitEnt.Discussions.SingleOrDefault(ent => ent.DiscussionID == ID);
                if (Discussion != null)
                    Discussion.IsEnabled = false;
            }
            int record = bitEnt.SaveChanges();
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Cập nhật " + record + " bài viết thành công!"
            });
            LoadGridData();
        }
        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            int record = 0;
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                DiscussionBLL.DeletePermanently(Convert.ToInt64(row.RecordID));
                record++;
            }
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Xóa " + record + " bình luận thành công!"
            });
            LoadGridData();
        }
        protected void btnDeleteAll_Click(object sender, DirectEventArgs e)
        {
            int record = 0;
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                DiscussionBLL.DeletePermanently(Convert.ToInt64(row.RecordID), true);
                record++;
            }
            Notification.Show(new NotificationConfig
            {
                Title = "Thông báo",
                Icon = Icon.Information,
                Html = "Xóa " + record + " bình luận thành công!"
            });
            LoadGridData();
        }

    }
}